import os
import interpolation
from utils import get_files, make_sure_path_exists, map_reduce
from star import lightcurve, plot
from scale import standardize
#from itertools import izip
import numpy

def main():
    options = get_options()
    files = get_files(options.input, options.format)#[:10]
    stars = map_reduce(lightcurve, files, options)
    if options.verbose:
        print "\nAnalyzing " + str(len(stars))\
                    + " of " + str(len(files)) + " stars"
    make_sure_path_exists(options.output)
    map_reduce(plot, stars, options)
    
    #x = numpy.arange(0, 1, 0.01)
    """
    evaluator = options.evaluator
    lcs = numpy.array([evaluator(star.coefficients) for star in stars])
    zs, means, stds = zip(*[standardize(col) for col in lcs.T])
    zs = numpy.array(zs).T
    map_reduce(plot, zs, options)
    eigvals, eigvecs = numpy.linalg.eig(numpy.cov(zs))
    print means, stds
    """

def get_options():
    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option('-i', '--input',      dest='input',       type='string',
                      help='location of stellar observations')
    parser.add_option('-f', '--format',     dest='format',      type='string',
      default='.dat', help='file format')
    parser.add_option('-o', '--output',     dest='output',      type='string',
                      help='desired output')
    parser.add_option('--interpolant',      dest='interpolant', type='string',
      default='poly', help='type of interpolation (poly, spline, trig, lin)')
    parser.add_option('-d', '--degree',     dest='degree',      type='int',
      default=10,     help='degree of interpolation')
    parser.add_option('--min_obs',          dest='min_obs',     type='int',
      default=100,    help='minimum number of observations per star')
    parser.add_option('--period_bins',      dest='period_bins', type='int',
      default=50000,  help='the size of the period space')
    parser.add_option('--min_period',       dest='min_period',  type='float',
      default=0.2,    help='minimum period of each star')
    parser.add_option('--max_period',       dest='max_period',  type='float',
      default=32.,    help='maximum period of each star')
    parser.add_option('-s', '--sigma',      dest='sigma',       type='float',
      default=1,      help='reject points sigma stds away from the mean')
    parser.add_option('-p', '--processors', dest='processors',  type='int',
      default=None,   help='number of processors to use')
    parser.add_option('-v', '--verbose',    dest='verbose', action='store_true',
      default=True,   help='verbose printing')
    parser.add_option('-q', '--quiet',      dest='verbose',action='store_false',
                      help='prevent printing')
    (options, args) = parser.parse_args()
    if options.input is None:
        parser.error('Need input file')
    if options.output is None:
        parser.error('Need output location')
    if options.interpolant in 'least_squares_polynomial':
        options.interpolant = interpolation.least_squares_polynomial
        options.evaluator = interpolation.polynomial_evaluator
    elif options.interpolant in 'spline':
        parser.error('Spline interpolation not yet implemented.')
        options.interpolant = interpolation.spline
        options.evaluator = interpolation.spline_evaluator
    elif options.interpolant in 'trigonometric':
        parser.error('Trigonometric interpolation not yet implemented.')
        options.interpolant = interpolation.trigonometric
        options.evaluator = interpolation.trigonometric_evaluator
    elif options.interpolant in 'piecewise_linear':
        parser.error('Piecewise linear interpolation not yet implemented.')
        options.interpolant = interpolation.piece_wise_linear
        options.evaluator = interpolation.piece_wise_linear_evaluator
    else:
        parser.error('Could not interpret ' + options.interpolant)
    return options

if __name__ == "__main__":
    """
    from sys import argv
    argv += ['-i', os.path.join('..', 'data', 'cep-f-i'),
             '-o', os.path.join('..', 'results', 'cep-f-i3'),
            '-p', '1']
    """
    main()
